package com.decheng.service.impl;

import com.decheng.dao.UserDao;
import com.decheng.dao.impl.UserDaoImpl;
import com.decheng.pojo.User;
import com.decheng.service.UserService;
import com.decheng.utils.MD5;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) {
        // 前台检查输入
        User user = new User();
        user.setUsername(username);
        User queryUser = userDao.getUserByName(user);
        if (queryUser != null) {
            if (queryUser.getPassword().equals(MD5.encrypt(password))) {
                return queryUser;
            }
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        // 1. 先查询用户名是否重复
        User queryUser = userDao.getUserByName(user);
        if (queryUser != null) {
            // 用户名重复直接返回false
            return false;
        }
        // 使用 md5 加密密码
        user.setPassword(MD5.encrypt(user.getPassword()));
        return userDao.saveUser(user);
    }
}
